// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class CowRun extends cc.Component {

    @property
    speed: number = 0;

    cowType: number = 0;

    canvasWidth: number;
    canvasHeight: number;

    onLoad () {
        let canvas = cc.find('Canvas');
        this.canvasWidth = canvas.width;
        this.canvasHeight = canvas.height;

        console.log('CowRun onLoad');
        this.cowType = Math.floor((Math.random()*3));
    }

    start () {
        console.log('CowRun start');
        this.playAnim();
    }

    playAnim() {
        let anim = this.getComponent(cc.Animation);
        
        this.cowType == 1;
        
        let animState = anim.play('cow2');
        // 使动画播放速度加速
        animState.speed = 2;
        // if (this.cowType == 0) {
        //     anim.play('cow1');
        // } else if (this.cowType == 1) {
        //     anim.play('cow2');
        // } else {
        //     anim.play('cow3');
        // }
    }

    update (dt) {
        this.node.x -= this.speed*dt;
        if (this.node.x < -this.canvasWidth) {
            this.node.removeFromParent();
        }
    }

    getCowType() {
        console.log('getCowType:' + this.cowType);
        return this.cowType;
    }
}
